<template>
  <el-card>
    <template #header> 添加账号 </template>
    <div class="UsersAdd-all">
      狠人阿凝
      <el-form
        :model="accountForm"
        :rules="rules"
        ref="ruleForm"
        label-width="160px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名称" prop="account">
          <el-input
            type="text"
            v-model="accountForm.account"
            autocomplete="off"
            placeholder="请输入用户名称"
            prefix-icon="iconfont icon-yonghu1"
          ></el-input>
        </el-form-item>

        <el-form-item label="用户密码" prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="accountForm.password"
            autocomplete="off"
            placeholder="请输入用户密码"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              >
              </i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="账号权限" prop="userGroup">
          <el-select
            v-model="accountForm.userGroup"
            clearable
            placeholder="请选择"
          >
            <el-option label="超级权限" value="1"></el-option>
            <el-option label="普通权限" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            确认添加
          </el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div>狗曹玉杰</div> -->
  </el-card>
</template>

<script>
import { userAdd } from '@/api/users.js'
export default {
  data() {
    var validateaccount = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名称'))
      } else {
        callback()
      }
    }
    var validatepassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validateuserGroup = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择用户权限'))
      } else {
        callback()
      }
    }
    return {
      accountForm: {
        account: '',
        password: '',
        userGroup: ''
      },
      rules: {
        account: [{ validator: validateaccount, trigger: 'blur' }],
        password: [{ validator: validatepassword, trigger: 'blur' }],
        userGroup: [{ validator: validateuserGroup, trigger: 'change' }]
      },
      flag: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 验证通过，可以添加用户
          // 获取本地数据的 token 和 member_id
          const users = window.localStorage.getItem('users') || '{}'
          const token = JSON.parse(users).access_token
          const { code, msg } = await userAdd({
            member_id: JSON.parse(users).member_id,
            access_token: JSON.parse(users).access_token,
            username: this.accountForm.account,
            password: this.accountForm.password,
            group_id: this.accountForm.userGroup - 0
          })
          if (code === 1) {
            // element-ui 提供了一个页面的提示组件
            this.$message.success(msg) // 固定格式
            setTimeout(() => {
              this.$router.push('/users/userslist')
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.flag = true
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  .UsersAdd-all {
    .el-form {
      width: 600px;
      .el-select {
        width: 440px;
      }
    }
  }
}
</style>
